﻿using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Domain.PM.select;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.ComponentModel.DataAnnotations;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.EweHistoryCommands
{
    public class EweHistorySelectHandler : IRequestHandler<EweHistorySelectCommand, Result>
    {
        private readonly IBaseRepository<SeedMotherHistoryRecord> historyRep;
        private readonly IBaseRepository<PregnancyTests> pregnancyRep;
        private readonly IBaseRepository<SleepInfor> sleepinforRep;

        public EweHistorySelectHandler(IBaseRepository<SeedMotherHistoryRecord> historyRep, IBaseRepository<PregnancyTests> pregnancyRep, IBaseRepository<SleepInfor> sleepinforRep)
        {
            this.historyRep = historyRep;
            this.pregnancyRep = pregnancyRep;
            this.sleepinforRep = sleepinforRep;
        }

        public async Task<Result> Handle(EweHistorySelectCommand request, CancellationToken cancellationToken)
        {
            var quest = new Result() { Code = (int)CodeEnum.success };

            //var list = (from a in historyRep.GetAll()
            //            join b in pregnancyRep.GetAll() on a.PregnancyCheckResult equals b.PregnancyTestResultName
            //            select new HistoryDTO()
            //            {
            //                SeedMotherHistoryRecordId = a.SeedMotherHistoryRecordId,
            //                SowId = a.SowId,
            //                MatingDate = a.MatingDate,
            //                BoarId = a.BoarId,
            //                PregnancyCheckDate = a.PregnancyCheckDate,
            //                FarrowingDate = a.FarrowingDate,
            //                HealthyLambsCount = a.HealthyLambsCount,
            //                HealthyL = a.HealthyL,
            //                weak_lambs_count = a.weak_lambs_count,
            //                deformed_lambs_count = a.deformed_lambs_count,
            //                stillborn_lambs_count = a.stillborn_lambs_count,
            //                mummified_lambs_count = a.mummified_lambs_count,
            //                malformed_fetus_count = a.malformed_fetus_count,
            //                WeanedQuantity = a.WeanedQuantity,
            //                TotalWeaningWeight = a.TotalWeaningWeight,
            //                WeanedDate = a.WeanedDate,
            //                WeanedNum = a.WeanedNum,
            //                PregnancyTestResult = b.PregnancyTestResult,
            //                PregnancyTestResultName = b.PregnancyTestResultName
            //            });
            var list = (from a in historyRep.GetAll()
                        join b in sleepinforRep.GetAll() on a.SeedMotherHistoryRecordId equals b.SleepInforId
                        select new HistoryDTO()
                        {
                            SeedMotherHistoryRecordId = a.SeedMotherHistoryRecordId,
                            SowId = a.SowId,
                            MatingDate = a.MatingDate,
                            BoarId = a.BoarId,
                            PregnancyCheckDate = a.PregnancyCheckDate,
                            FarrowingDate = a.FarrowingDate,
                            HealthyLambsCount = a.HealthyLambsCount,
                            HealthyL = a.HealthyL,
                            weak_lambs_count = a.weak_lambs_count,
                            deformed_lambs_count = a.deformed_lambs_count,
                            stillborn_lambs_count = a.stillborn_lambs_count,
                            mummified_lambs_count = a.mummified_lambs_count,
                            malformed_fetus_count = a.malformed_fetus_count,
                            WeanedQuantity = a.WeanedQuantity,
                            TotalWeaningWeight = a.TotalWeaningWeight,
                            WeanedDate = a.WeanedDate,
                            WeanedNum = a.WeanedNum,
                            SleepInforId=b.SleepInforId,
                            MoEarNumber = b.MoEarNumber,
                            FarEarNumber = b.FarEarNumber,
                        });

            if (request.SleepInforId != 0)
            {
                list = list.Where(x => x.SleepInforId == request.SleepInforId);
            }

            if (!string.IsNullOrEmpty(request.start))
            {
                list = list.Where(x => x.MatingDate >= DateTime.Parse(request.start));
            }
            if (!string.IsNullOrEmpty(request.end))
            {
                list = list.Where(x => x.MatingDate < DateTime.Parse(request.end));
            }

            quest.Code = (int)CodeEnum.success;
            quest.Data = list;

            return quest;

        }




        public class HistoryDTO
        {
            /// <summary>
            /// 种母历史记录的ID
            /// </summary>
            public int SeedMotherHistoryRecordId { get; set; }
            /// <summary>
            /// 种母耳标号
            /// </summary>
            public int SowId { get; set; }

            /// <summary>
            /// 配种日期
            /// </summary>
            public DateTime MatingDate { get; set; }

            /// <summary>
            /// 种公耳标号
            /// </summary>
            public int BoarId { get; set; }

            /// <summary>
            /// 妊娠检查日期
            /// </summary>
            public DateTime PregnancyCheckDate { get; set; }

            /// <summary>
            /// 妊娠检查结果（如：妊娠、未妊娠）
            /// </summary>
            public string PregnancyCheckResult { get; set; }

            /// <summary>
            /// 分娩日期
            /// </summary>
            public DateTime FarrowingDate { get; set; }

            /// <summary>
            /// 出生的健壮羔羊数
            /// </summary>
            public int HealthyLambsCount { get; set; }

            /// <summary>
            /// 健壮羔羊的总重量
            /// </summary>
            public decimal HealthyL { get; set; }

            /// <summary>
            /// 弱羔数
            /// </summary>
            public int weak_lambs_count { get; set; }
            /// <summary>
            /// 畸形数
            /// </summary>
            public int deformed_lambs_count { get; set; }
            /// <summary>
            /// 死胎数
            /// </summary>
            public int stillborn_lambs_count { get; set; }
            /// <summary>
            /// 木乃伊数
            /// </summary>
            public int mummified_lambs_count { get; set; }
            /// <summary>
            /// 畸胎数
            /// </summary>
            public int malformed_fetus_count { get; set; }
            /// <summary>
            /// 断奶只数
            /// </summary>
            public int WeanedQuantity { get; set; }
            /// <summary>
            /// 断奶总窝重（公斤）
            /// </summary>
            public decimal TotalWeaningWeight { get; set; }
            /// <summary>
            /// 断奶日期
            /// </summary>
            public DateTime WeanedDate { get; set; }
            /// <summary>
            /// 当前胎次
            /// </summary>
            public int WeanedNum { get; set; }


            /// <summary>
            /// 父耳号
            /// </summary>
            public string? FarEarNumber { get; set; }
            /// <summary>
            /// 母耳号
            /// </summary>
            public string? MoEarNumber { get; set; }
            public int SleepInforId { get; set; }//档案编号
        }
    }
}
